<?php


include_once './api.php';


/**
 * 事件初始化类
 */
class HandlerEventMsg
{
    
    static public function handler($msgxml)
    {
        de("\n-----------event---------\n");
        switch ($msgxml->Event) {
            case 'scancode_waitmsg':
                $msg = new ScanCodeWaitMsg($msgxml);
                break;
            
            default:
                # code...
                break;
        }

        if (isset($msg)) {
            static::action($msg);
        }
    }

    /**
     * 事件,响应消息
     */
    static public function action($msg)
    {
        if ($msg) {
            $msg->respon();
        }
    }
}


/**
 * 事件消息
 */
abstract class EventMsg extends WechatReceiveMsg
{
    public $event;      // 事件类型
    public $eventKey;   // 事件KEY值，由开发者在创建菜单时设定

    /**
     * 初始化事件消息
     */
    public function __construct($msgxml)
    {
        parent::__construct($msgxml);
        $this->event = $msgxml->Event;
        $this->eventKey = $msgxml->EventKey;
    }

    abstract public function respon();
}

/**
 * 扫码等待事件:scancode_waitmsg
 */
class ScanCodeWaitMsg extends EventMsg
{
    public $scanType;       // 扫描类型，一般是qrcode
    public $scanResult;     // 扫描结果，即二维码对应的字符串信息
    
    public function __construct($msgxml)
    {
        parent::__construct($msgxml);
        $this->makeContent();
    }

    /**
     * 点击事件处理
     */
    public function makeContent()
    {
        $this->scanType = $this->msgxml->ScanCodeInfo->ScanType;
        $this->scanResult = $this->msgxml->ScanCodeInfo->ScanResult;
    }

    /**
     * 回应消息 快递接口
     */
    public function respon()
    {
        $content = APITool::getCourier($this->scanResult);
        $data = [
            'from' =>$this->toUserName,
            'to'   =>$this->fromUserName,
            'type' =>'text',
            'content'=>$content
        ];
        $msg = new WechatSendTextMsg($data);
        $msg->responseMsg();
    }
}